﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;

namespace Net.Helper
{
    public class CertificateHelper
    {
        /// <summary>
        /// 开发调式阶段证书数据
        /// </summary>
        public readonly static byte[] CertificateData = new byte[]
        {
            48, 130, 6, 82, 2, 1, 3, 48, 130, 6, 14, 6, 9, 42, 134, 72, 134, 247, 13, 1,
            7, 1, 160, 130, 5, 255, 4, 130, 5, 251, 48, 130, 5, 247, 48, 130, 3, 72, 6, 9,
            42, 134, 72, 134, 247, 13, 1, 7, 1, 160, 130, 3, 57, 4, 130, 3, 53, 48, 130, 3,
            49, 48, 130, 3, 45, 6, 11, 42, 134, 72, 134, 247, 13, 1, 12, 10, 1, 2, 160, 130,
            2, 166, 48, 130, 2, 162, 48, 28, 6, 10, 42, 134, 72, 134, 247, 13, 1, 12, 1, 3,
            48, 14, 4, 8, 156, 192, 41, 120, 63, 219, 16, 117, 2, 2, 7, 208, 4, 130, 2, 128,
            88, 201, 207, 202, 100, 44, 199, 214, 193, 203, 212, 32, 196, 144, 100, 112, 14, 81, 39, 97,
            80, 128, 25, 28, 192, 102, 51, 230, 65, 71, 120, 172, 197, 135, 70, 210, 26, 110, 231, 27,
            4, 79, 181, 187, 236, 96, 36, 110, 213, 162, 229, 82, 213, 184, 153, 116, 249, 71, 157, 94,
            145, 90, 151, 92, 161, 233, 164, 112, 152, 123, 137, 203, 39, 174, 166, 201, 204, 160, 140, 140,
            210, 151, 49, 81, 186, 104, 85, 119, 54, 7, 202, 184, 93, 6, 29, 123, 197, 166, 224, 99,
            157, 159, 83, 100, 104, 114, 86, 191, 77, 185, 150, 54, 197, 35, 151, 241, 221, 39, 177, 70,
            228, 73, 120, 209, 185, 5, 33, 180, 172, 117, 70, 202, 40, 69, 60, 139, 199, 203, 88, 178,
            93, 88, 11, 62, 191, 185, 136, 230, 141, 60, 231, 188, 2, 115, 15, 111, 130, 231, 195, 59,
            147, 105, 117, 82, 87, 104, 226, 92, 104, 48, 173, 21, 109, 193, 189, 194, 88, 151, 248, 226,
            32, 121, 183, 117, 154, 203, 101, 235, 152, 17, 91, 208, 101, 126, 207, 66, 178, 26, 165, 241,
            176, 38, 58, 172, 181, 88, 239, 148, 132, 179, 143, 71, 89, 165, 165, 98, 82, 250, 40, 155,
            4, 172, 124, 58, 31, 164, 142, 130, 126, 208, 50, 125, 37, 205, 198, 68, 67, 127, 117, 105,
            198, 51, 135, 223, 78, 60, 74, 98, 60, 113, 107, 108, 123, 246, 117, 97, 129, 23, 42, 96,
            38, 87, 78, 86, 165, 144, 177, 96, 22, 218, 254, 219, 204, 243, 11, 51, 99, 166, 188, 89,
            78, 117, 177, 147, 14, 78, 144, 239, 26, 19, 232, 221, 204, 76, 79, 211, 68, 219, 195, 188,
            119, 204, 202, 177, 233, 106, 94, 191, 146, 4, 124, 28, 134, 172, 190, 56, 83, 235, 199, 242,
            21, 24, 79, 14, 148, 45, 13, 50, 19, 139, 234, 125, 13, 153, 42, 212, 134, 128, 8, 207,
            186, 64, 51, 152, 106, 251, 93, 49, 232, 104, 145, 190, 126, 213, 53, 114, 235, 78, 23, 64,
            187, 89, 251, 185, 236, 49, 127, 100, 158, 165, 16, 213, 81, 52, 64, 171, 201, 72, 163, 100,
            50, 220, 120, 37, 55, 34, 213, 12, 120, 98, 15, 67, 30, 102, 58, 11, 19, 118, 102, 163,
            183, 68, 249, 166, 11, 233, 112, 195, 209, 213, 226, 113, 39, 27, 163, 80, 235, 219, 250, 89,
            100, 138, 16, 123, 56, 166, 57, 217, 121, 149, 242, 105, 252, 6, 213, 20, 212, 92, 130, 144,
            201, 255, 241, 172, 37, 11, 8, 188, 214, 85, 134, 29, 237, 175, 39, 71, 46, 84, 87, 174,
            251, 252, 212, 134, 130, 116, 119, 70, 232, 204, 122, 34, 82, 116, 212, 62, 59, 51, 9, 151,
            31, 176, 28, 47, 138, 85, 37, 150, 30, 196, 149, 130, 6, 38, 84, 98, 245, 211, 176, 225,
            58, 239, 142, 15, 6, 75, 116, 92, 0, 91, 27, 152, 169, 38, 57, 25, 211, 172, 120, 171,
            217, 141, 235, 226, 154, 255, 113, 119, 19, 173, 166, 112, 131, 14, 214, 63, 201, 191, 147, 184,
            197, 88, 46, 16, 221, 109, 139, 60, 196, 113, 125, 213, 231, 24, 185, 194, 240, 225, 232, 173,
            72, 171, 199, 134, 175, 132, 198, 148, 165, 93, 107, 194, 38, 108, 46, 105, 160, 12, 202, 102,
            217, 107, 134, 0, 104, 170, 215, 159, 8, 56, 225, 71, 178, 221, 180, 78, 127, 23, 248, 155,
            11, 43, 57, 107, 168, 146, 161, 240, 195, 88, 17, 168, 92, 34, 61, 80, 101, 75, 140, 7,
            244, 230, 80, 159, 200, 225, 78, 7, 63, 183, 255, 142, 139, 203, 152, 99, 81, 41, 77, 29,
            49, 116, 48, 19, 6, 9, 42, 134, 72, 134, 247, 13, 1, 9, 21, 49, 6, 4, 4, 1,
            0, 0, 0, 48, 93, 6, 9, 43, 6, 1, 4, 1, 130, 55, 17, 1, 49, 80, 30, 78,
            0, 77, 0, 105, 0, 99, 0, 114, 0, 111, 0, 115, 0, 111, 0, 102, 0, 116, 0, 32,
            0, 83, 0, 111, 0, 102, 0, 116, 0, 119, 0, 97, 0, 114, 0, 101, 0, 32, 0, 75,
            0, 101, 0, 121, 0, 32, 0, 83, 0, 116, 0, 111, 0, 114, 0, 97, 0, 103, 0, 101,
            0, 32, 0, 80, 0, 114, 0, 111, 0, 118, 0, 105, 0, 100, 0, 101, 0, 114, 48, 130,
            2, 167, 6, 9, 42, 134, 72, 134, 247, 13, 1, 7, 6, 160, 130, 2, 152, 48, 130, 2,
            148, 2, 1, 0, 48, 130, 2, 141, 6, 9, 42, 134, 72, 134, 247, 13, 1, 7, 1, 48,
            28, 6, 10, 42, 134, 72, 134, 247, 13, 1, 12, 1, 3, 48, 14, 4, 8, 130, 235, 59,
            141, 229, 41, 187, 205, 2, 2, 7, 208, 128, 130, 2, 96, 119, 227, 190, 105, 146, 39, 20,
            188, 243, 206, 160, 34, 202, 26, 168, 170, 161, 63, 202, 190, 176, 110, 164, 71, 141, 119, 51,
            1, 39, 106, 108, 35, 57, 9, 4, 166, 242, 196, 229, 170, 71, 101, 16, 121, 247, 109, 61,
            49, 116, 244, 10, 214, 25, 58, 91, 122, 36, 148, 208, 234, 151, 59, 14, 184, 248, 94, 127,
            166, 45, 194, 9, 141, 166, 177, 96, 40, 183, 5, 150, 162, 80, 233, 132, 198, 70, 100, 11,
            251, 245, 210, 204, 244, 197, 245, 132, 53, 156, 32, 246, 153, 225, 60, 147, 114, 51, 50, 73,
            215, 24, 231, 124, 99, 118, 61, 146, 108, 180, 85, 53, 144, 189, 77, 173, 18, 137, 203, 228,
            39, 152, 122, 100, 187, 17, 167, 180, 5, 65, 32, 151, 6, 7, 9, 190, 21, 148, 45, 61,
            239, 249, 119, 137, 134, 40, 156, 93, 226, 126, 219, 65, 153, 178, 84, 225, 1, 195, 125, 142,
            92, 167, 214, 215, 230, 164, 78, 230, 27, 152, 112, 158, 24, 195, 124, 5, 103, 217, 37, 39,
            66, 53, 32, 4, 182, 127, 41, 166, 25, 94, 49, 41, 241, 3, 0, 151, 223, 103, 230, 229,
            207, 182, 86, 159, 241, 253, 190, 21, 139, 202, 183, 8, 205, 95, 8, 250, 17, 227, 97, 228,
            70, 171, 59, 146, 25, 71, 193, 208, 175, 119, 191, 24, 248, 54, 115, 212, 235, 6, 163, 162,
            51, 36, 141, 237, 209, 198, 152, 227, 214, 227, 28, 14, 140, 195, 79, 42, 124, 129, 230, 88,
            36, 62, 168, 82, 171, 186, 19, 233, 88, 7, 245, 176, 66, 9, 232, 107, 147, 237, 194, 17,
            253, 99, 197, 6, 235, 42, 82, 238, 11, 205, 164, 209, 81, 210, 88, 38, 227, 127, 78, 43,
            158, 224, 38, 72, 165, 241, 37, 204, 239, 109, 159, 80, 242, 4, 254, 26, 105, 131, 87, 252,
            23, 234, 31, 249, 43, 178, 67, 107, 41, 208, 76, 124, 234, 251, 117, 166, 130, 101, 177, 53,
            238, 247, 136, 8, 20, 151, 215, 123, 101, 177, 141, 137, 54, 125, 180, 157, 248, 167, 121, 248,
            104, 227, 181, 82, 135, 1, 203, 159, 119, 223, 28, 12, 147, 128, 68, 110, 232, 126, 28, 245,
            151, 160, 171, 177, 100, 211, 81, 240, 211, 18, 245, 138, 5, 128, 157, 206, 144, 29, 140, 181,
            36, 140, 156, 206, 59, 164, 243, 12, 43, 10, 216, 93, 201, 118, 175, 124, 26, 151, 77, 181,
            210, 90, 170, 14, 72, 57, 187, 99, 24, 179, 187, 119, 231, 114, 93, 190, 213, 155, 93, 27,
            104, 57, 244, 40, 145, 115, 59, 124, 26, 154, 42, 1, 157, 218, 10, 221, 100, 225, 98, 58,
            159, 131, 252, 131, 247, 188, 232, 52, 175, 186, 186, 119, 94, 73, 146, 94, 16, 78, 183, 187,
            214, 110, 106, 248, 253, 237, 170, 229, 35, 230, 4, 230, 125, 204, 194, 46, 13, 222, 10, 174,
            131, 169, 118, 70, 227, 116, 209, 117, 52, 11, 122, 175, 94, 113, 94, 140, 54, 161, 174, 1,
            162, 95, 27, 169, 66, 135, 87, 190, 232, 83, 109, 0, 137, 85, 178, 81, 9, 18, 23, 215,
            186, 219, 186, 197, 181, 216, 204, 221, 38, 46, 235, 78, 72, 131, 19, 14, 128, 147, 196, 53,
            213, 247, 111, 241, 195, 204, 93, 238, 198, 63, 48, 250, 155, 179, 237, 112, 2, 24, 234, 178,
            165, 62, 53, 15, 155, 56, 143, 39, 65, 212, 42, 239, 82, 41, 67, 221, 71, 140, 75, 237,
            252, 48, 59, 48, 31, 48, 7, 6, 5, 43, 14, 3, 2, 26, 4, 20, 90, 29, 87, 100,
            5, 32, 155, 117, 48, 243, 220, 28, 63, 231, 208, 33, 154, 103, 169, 30, 4, 20, 189, 194,
            137, 100, 135, 99, 29, 100, 3, 237, 115, 109, 37, 246, 242, 126, 171, 95, 24, 218, 2, 2,
            7, 208
        };

        public static X509Certificate2 CreateX509Certificate2()
        {
            var certData = CreateX509Certificate2("gdnet_websocket_certificate", "gdnet_certificate");
            return new X509Certificate2(certData, "gdnet_certificate");
        }

        public static void CreateX509Certificate2(string subjectName, string password, string savePath)
        {
            var certData = CreateX509Certificate2(subjectName, password);
            File.WriteAllBytes(Path.Combine(savePath, subjectName + ".pfx"), certData);
        }

        public static byte[] CreateX509Certificate2(string subjectName, string password)
        {
#if UNITY_2021_1_OR_NEWER || SERVICE
            // 创建一个新的RSA密钥对，用于证书
            using (RSA rsa = RSA.Create())
            {
                var request = new CertificateRequest($"CN={subjectName}", rsa, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
                // 设置证书的有效期
                request.CertificateExtensions.Add(new X509BasicConstraintsExtension(false, false, 0, false));
                // 创建自签名证书
                var certificate = request.CreateSelfSigned(DateTimeOffset.Now.AddDays(-1), DateTimeOffset.Now.AddYears(1));
                // 将证书导出到文件（可选）
                var certData = certificate.Export(X509ContentType.Pkcs12, password);
                return certData;
            }
#else
            return null;
#endif
        }

        public static X509Certificate2 GetDefaultCertificate()
        {
            return new X509Certificate2(CertificateData, "gdnet_certificate");
        }
    }
}
